<template>

  <div style="margin-top: 60px;margin-left:330px;width: 300px;height: 500px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="原密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="newPassword">
        <el-input v-model="ruleForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="ruleForm.confirmPassword"></el-input>
      </el-form-item>


      <el-form-item>

        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>


export default {
  name: "UpdateOneself",
  data() {
    return {
      category: null,
      ruleForm: {
        nickname: '',

        password: '',
        newPassword: '',
        confirmPassword: '',
        id: 0,

      },
      userList: '',
      rules: {
        nickname: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'}
        ],

        password: [
          {
            required: true, message: '请输入原密码', trigger: 'blur'
          },
        ],

        confirmPassword: [{required: true, message: "请再次输入密码", trigger: "blur"},
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.id = Number(this.ruleForm.id);
          if(this.ruleForm.newPassword!==this.ruleForm.confirmPassword){
            this.$notify.error({
              title: '错误',
              message: '确认密码失败',
            });
            return
          }

          axios.post('http://localhost:8082/user/updateOneself',_this.ruleForm).then(function (resp) {

            if (resp.data.code === 200) {
              _this.$notify({
                title: '成功',
                message: '更改成功',
                type: 'success'
              });
              _this.$router.push('/login');

            }
            if (resp.data.code === 500) {

              this.$notify.error({
                title: '错误',
                message: resp.data.msg,
              });
            }



          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

  created() {
    const _this = this
    let admin = JSON.parse(localStorage.getItem('access-admin'));
    let id = admin.id
    console.log(id)
    axios.get('http://localhost:8082/manager/findById/' + id).then(function (resp) {
      _this.ruleForm.id = resp.data.data.id
      _this.ruleForm.nickname = resp.data.data.nickname;
    }).catch(error => {
      if (error.response) {
        // 请求发送成功，但服务器返回错误状态码
        console.log('Error status:', error.response.status);
        console.log('Error data:', error.response.data);
      } else if (error.request) {
        // 请求发送失败
        console.log('Request error:', error.request);
      } else {
        // 其他类型的错误
        console.log('Error message:', error.message);
      }

    })
  }

}
</script>
